<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
session_start();

class chat extends CI_Controller {

    private $logged_in = false;

    public function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in_user')) {
            $this->logged_in = TRUE;
        } else {
            redirect('login/', 'refresh');
        }
        $this->load->model('Dao');
        $this->load->helper('date');
    }

    public function ajaxchat() {
        $this->load->view("ajax-chat");
    }

    public function index() {
        $data = array('user' => $this->session->userdata('logged_in_user'));
        $user = $this->Dao->findUserById($data['user']->id);
        $data['user'] = $user;
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['notifs'] = 'v_notifs';
        $data['view'] = 'v_chat';
        $data['users'] = Array(); //$this->Dao->findUsers($user->id);

        $connections = "";
        $data['online_friends'] = $this->Dao->findOnlineFriends($data['user']);

        if ($this->session->userdata('connections')) {
            $connections = $this->session->userdata('connections');
            $data['friends'] = $this->Dao->findConnections($connections);
        } else {
            $data['friends'] = array();
        }

        $data['notifsNumber'] = $this->Dao->findNotifsNbr($user->id);
        $data['connections'] = $connections;
        $data['last_with'] = $this->Dao->findLastUserChatingWith();
        $this->load->view('templates/template', $data);
    }

    public function with() {
        $id = $this->input->post("withId");

        $to_user = $this->Dao->findUserById($id);

        if ($to_user == null) {
            redirect('Login/', 'refresh');
            return;
        }
        $data = array('user' => $this->session->userdata('logged_in_user'), 'to_user' => $to_user);
        $user = $data['user'];
        $userId = $user->id;
        $data['discussion'] = $this->Dao->findDiscussion($userId, $id);
        $data['networks'] = $this->Dao->findNetworksByUser($to_user);

        $discussions_size = sizeof($data['discussion']);
        if ($discussions_size > 0) {
            $data['dtMin'] = $data['discussion'][0]->dt_complete;
        }

        //Add as connection :
        if ($this->session->userdata('connections')) {
            if (strpos($this->session->userdata('connections'), $id) !== true) {
                $this->session->set_userdata('connections', $this->session->userdata('connections') . $id . ",");
            }
        } else {
            $this->session->set_userdata('connections', $id . ",");
        }

        echo json_encode($data);
    }

    public function plus($dt_complete) {
        $from_user = $this->input->post("withId");
        $to_user = $this->Dao->findUserById($from_user);
        if ($to_user == null) {
            redirect('Login/', 'refresh');
            return;
        }
        $data = array('user' => $this->session->userdata('logged_in_user'), 'to_user' => $to_user);
        $user = $data['user'];
        $userId = $user->id;
        $data['discussion'] = $this->Dao->findDiscussionsPlus($dt_complete, $userId, $from_user);
        $data['networks'] = $this->Dao->findNetworksByUser($to_user);

        $discussions_size = sizeof($data['discussion']);
        if ($discussions_size > 0) {
            $data['dtMin'] = $data['discussion'][0]->dt_complete;
        }
        else
            $data['dtMin'] = 'empty';

        echo json_encode($data);
    }

    public function send() {
        $message = $this->input->post('message');
        $to = $this->input->post('with');
        $user = $this->session->userdata('logged_in_user');
        $userId = $user->id;

        $dataToInsert = array(
            'id' => 'null',
            'dt' => date('Y-m-d H:i:s', now()),
            'message' => $message,
            'from_user' => $userId,
            'to_groupe' => null,
            'to_user' => $to
        );
        $this->Dao->insertDiscussion($dataToInsert);
        $idInsertedDiscussion = $this->db->insert_id();

        //Insertion of the notification
        $dataNotifs = array(
            'id' => '',
            'from_user' => $userId,
            'to_user' => $to,
            'action' => '1',
            'read' => '0',
            'object' => 'discussion',
            'object_id' => $idInsertedDiscussion,
            'dt_notifs' => date('Y-m-d H:i:s', now())
        );
        $this->Dao->insertNotification($dataNotifs);
//        $dataUserNotifs = array(
//            'id' => 'null',
//            'user' => $to,
//            'notifications' => '1'
//        );
//        $this->Dao->UserNotifsPlus($dataUserNotifs);

        $data = array('discussion' => $this->Dao->findDiscussionById($idInsertedDiscussion, false)
            , 'status' => $this->Dao->findStatutsForConnectionsOf());

        echo json_encode($data);
    }

    public function getNews($withId) {
        $user = $this->session->userdata('logged_in_user');
        $notifs = $this->Dao->findNotifs($user->id, $withId);
        echo json_encode($notifs);
    }

    public function delNotifs() {
        $user = $this->session->userdata('logged_in_user');
        $userId = $user->id;
        $notifsDel = $this->input->post("notifsDel");
        $nbr = $this->Dao->delNotifs($notifsDel, $userId);
        echo $nbr;
    }

    public function users() {
        $data = array('user' => $this->session->userdata('logged_in_user'));
        $user = $data['user'];
        $data['title'] = 'Welcome to wfaces';
        $data['description'] = 'NONE';
        $data['keywords'] = 'Key, words, of, the, page';
        $data['view'] = 'v_chat2';
        $data['left_view'] = 'v_users';
        $data['h_view'] = 'header';
        $data['users'] = $this->Dao->findUsers($user->id);
        $data['notifsNumber'] = $this->Dao->findNotifsNbr($user->id);
        $data['online_friends'] = $this->Dao->findOnlineFriends($data['user']);

        $this->load->view('v_users', $data);
    }

}

?>
